/*
 *  Copyright 2009 Jan-Kees van Andel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.jkva.jsf.components.nonvalidatingcommand.examples;

import java.util.ArrayList;
import java.util.List;
import java.util.ConcurrentModificationException;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 * Simple database.
 * </p>
 *
 * @author Jan-Kees van Andel
 */
public class DatabaseMock {

    private static final AtomicLong sequence = new AtomicLong(0);

    private static final List<Company> companies;
    static {
        companies = new CopyOnWriteArrayList<Company>();
        companies.add(new Company(sequence.incrementAndGet(), "Ordina 1"));
        companies.add(new Company(sequence.incrementAndGet(), "Ordina 2"));
        companies.add(new Company(sequence.incrementAndGet(), "Ordina 3"));
        companies.add(new Company(sequence.incrementAndGet(), "Ordina 4"));
    }

    private static final List<Employee> employees;
    static {
        employees = new CopyOnWriteArrayList<Employee>();
        employees.add(new Employee(sequence.incrementAndGet(), "JK 1", getCompany(1)));
        employees.add(new Employee(sequence.incrementAndGet(), "JK 2", getCompany(1)));
        employees.add(new Employee(sequence.incrementAndGet(), "JK 3", getCompany(1)));
        employees.add(new Employee(sequence.incrementAndGet(), "JK 4", getCompany(1)));
        employees.add(new Employee(sequence.incrementAndGet(), "JK 5", getCompany(1)));
        employees.add(new Employee(sequence.incrementAndGet(), "JK 6", getCompany(2)));
        employees.add(new Employee(sequence.incrementAndGet(), "JK 7", getCompany(2)));
        employees.add(new Employee(sequence.incrementAndGet(), "JK 8", getCompany(2)));
        employees.add(new Employee(sequence.incrementAndGet(), "JK 9", getCompany(3)));
    }

    public static List<Company> getCompanies() {
        List<Company> ret = new ArrayList<Company>(companies.size());
        for (Company c : companies) {
            ret.add(new Company(c.getId(), c.getName()));
        }
        return ret;
    }

    public static List<Employee> getEmployees() {
        List<Employee> ret = new ArrayList<Employee>(employees.size());
        for (Employee e : employees) {
            Company company = getCompany(e.getCompany().getId());
            ret.add(new Employee(e.getId(), e.getName(), e.getAddress(), e.getBirthdate(), company));
        }
        return ret;
    }

    public static Company getCompany(long id) {
        for (Company company : companies) {
            if (id == company.getId()) {
                return new Company(company.getId(), company.getName());
            }
        }
        return null;
    }

    public static Employee getEmployee(long id) {
        for (Employee employee : employees) {
            if (id == employee.getId()) {
                return new Employee(employee.getId(), employee.getName(), employee.getCompany());
            }
        }
        return null;
    }

    public static void addEmployee(Employee employee) {
        employee.setId(sequence.incrementAndGet());
        employees.add(employee);
    }

    public static void editEmployee(Employee employee) {
        long id = employee.getId();
        Employee e = null;
        int i = -1;
        for (Employee iter : employees) {
            i++;
            if (id == iter.getId()) {
                e = iter;
                break;
            }
        }
        if (e != null) {
            employees.set(i, employee);
        } else {
            throw new ConcurrentModificationException("Non existent employee");
        }
    }
}
